package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Glfy;
import com.hnkywl.sts.entity.cggl.Jsd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-2
 * Time: 下午4:16
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class WljsdDao extends SimpleHibernateTemplate<Jsd, Long> {
    public Page<Jsd> findWljsdList(Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jsd> page = new Page<Jsd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jsdh != null && !jsdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jsdh like ? ");
            param.add("%" + jsdh.trim() + "%");
        }
        if (sqrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq >= ?");
            param.add(sqrqStart);
        }
        if (sqrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq <= ?");
            param.add(sqrqEnd);
        }
        if (glqyId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.id = ? ");
            param.add(glqyId);
        }
        if (zt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
        if (glqyQc != null && !"".equals(glqyQc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.qc like ? ");
            param.add("%" + glqyQc.trim() + "%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(gsId);
        }
        if (gsJc != null && !"".equals(gsJc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsJc.trim() + "%");
        }
        if (fpzlId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fpzl = ? ");
            param.add(fpzlId);
        }
        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" type = ? ");
        param.add(2);

        //==============记录数量===============
        hql = "select count(*) from Jsd where sftzd!=1 and deleted!=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jsd where sftzd!=1 and deleted!=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
    public List<Map> findWljsdDetail(Long wljsdId) {
        Criteria criteria = getSession().createCriteria(Glfy.class)
                .createAlias("cgd", "cgd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsd", "xsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("sydw", "sydw", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fylb", "fylb", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("cgd.cgdh").as("cgdh"))
                        .add(Property.forName("xsd.xsdh").as("xsdh"))
                        .add(Property.forName("sydw.id").as("sydw.id"))
                        .add(Property.forName("sydw.qc").as("sydw.qc"))
                        .add(Property.forName("fylb.id").as("fylb.id"))
                        .add(Property.forName("fylb.fykmjc").as("fylb.fykmjc"))
                        .add(Property.forName("jfsl").as("jfsl"))
                        .add(Property.forName("dj").as("dj"))
                        .add(Property.forName("je").as("je"))
                        .add(Property.forName("hdsl").as("hdsl"))
                        .add(Property.forName("hddj").as("hddj"))
                        .add(Property.forName("hdje").as("hdje"))
                        .add(Property.forName("kdse").as("kdse"))
                        .add(Property.forName("se").as("se"))
                        .add(Property.forName("me").as("me"))
                        .add(Property.forName("ce").as("ce"))
                        .add(Property.forName("jsje").as("jsje"))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("wljsd.id", wljsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return criteria.list();
    }

}
